#include "collide.h"

static void operate_on_ennemy(s_enn_list *el, s_object *object)
{
    el->life -= 1;
    if (el->life <= 0)
    {
        object->gold += 10;
        del_from_enn_list(el->id, &object->enn_list);
    }
}

void check_collide(s_object *object)
{
    s_enn_list *el = NULL;
    s_tow_list *tl = NULL;
    int i;
    int j;

    tl = object->tow_list;
    while (tl)
    {
        el = object->enn_list;
        while (el)
        {
            for (i = -1; i <= 1; i++)
                for (j = -1; j <= 1; j++)
                {
                    if (tl->pos[0] == el->pos[0] + i &&
                        tl->pos[1] == el->pos[1] + j)
                    {
                        operate_on_ennemy(el, object);
                    }
                }
            el = el->next;
        }
        tl = tl->next;
    }
}
